package com.ruoyi.web.controller.rjgc;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.littleRedBook.domain.pojo.Order;
import com.ruoyi.littleRedBook.domain.pojo.Review;
import com.ruoyi.littleRedBook.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order")
@Api(tags = "订单管理")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/create")
    @ApiOperation("生成订单")
    public AjaxResult createOrder(@ApiParam("订单信息（需包含productId、addressId）") @RequestBody Order order) {
        return orderService.createOrder(order);
    }

    @GetMapping("/list/pending-pay")
    @ApiOperation("获取当前用户的待付款订单列表")
    public AjaxResult getPendingPayOrders(
            @ApiParam("页码，默认1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页条数，默认10") @RequestParam(defaultValue = "10") Integer pageSize) {
        return orderService.getOrdersByStatus(0, pageNum, pageSize);
    }

    @GetMapping("/list/pending-ship")
    @ApiOperation("获取当前用户的待发货订单列表（卖家视角）")
    public AjaxResult getPendingShipOrders(
            @ApiParam("页码，默认1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页条数，默认10") @RequestParam(defaultValue = "10") Integer pageSize) {
        return orderService.getSellerOrdersByStatus(1, pageNum, pageSize);
    }

    @GetMapping("/list/pending-receive")
    @ApiOperation("获取当前用户的待收货订单列表（买家视角）")
    public AjaxResult getPendingReceiveOrders(
            @ApiParam("页码，默认1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页条数，默认10") @RequestParam(defaultValue = "10") Integer pageSize) {
        return orderService.getOrdersByStatus(2, pageNum, pageSize);
    }

    @PutMapping("/confirm-receive/{orderId}")
    @ApiOperation("确认收货")
    public AjaxResult confirmReceive(@ApiParam("订单ID") @PathVariable Long orderId) {
        return orderService.confirmReceive(orderId);
    }

    @PutMapping("/cancel/{orderId}")
    @ApiOperation("取消订单")
    public AjaxResult cancelOrder(
            @ApiParam("订单ID") @PathVariable Long orderId,
            @ApiParam("取消原因") @RequestParam String cancelReason) {
        return orderService.cancelOrder(orderId, cancelReason);
    }

    @GetMapping("/detail/{orderId}")
    @ApiOperation("获取订单详情")
    public AjaxResult getOrderDetail(@ApiParam("订单ID") @PathVariable Long orderId) {
        return orderService.getOrderDetail(orderId);
    }

    @PostMapping("/review")
    @ApiOperation("订单评价（仅已完成订单可评价）")
    public AjaxResult addOrderReview(@ApiParam("评价信息") @RequestBody Review review) {
        return orderService.addOrderReview(review);
    }

    @GetMapping("/review/{orderId}")
    @ApiOperation("获取订单对应的评价")
    public AjaxResult getOrderReview(@ApiParam("订单ID") @PathVariable Long orderId) {
        return orderService.getOrderReview(orderId);
    }
}
